#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "DevicesUID.h"
#include "parameters.h"


ParametersType g_typeParameters = {0};


int8_t cParametersSave(void)
{
    nvs_handle_t nvs_handle;

    nvs_open("Parameters", NVS_READWRITE, &nvs_handle);

    nvs_set_str(nvs_handle, "bt_name",          g_typeParameters.BT_NAME);

    nvs_set_str(nvs_handle, "wifi_sta_ssid",    g_typeParameters.WIFI_STA_SSID);
    nvs_set_str(nvs_handle, "wifi_sta_passwd",  g_typeParameters.WIFI_STA_PASSWORD);
    nvs_set_str(nvs_handle, "wifi_ap_ssid",     g_typeParameters.WIFI_AP_SSID);
    nvs_set_str(nvs_handle, "wifi_ap_passwd",   g_typeParameters.WIFI_AP_PASSWORD);

    nvs_set_str(nvs_handle, "app_uid",          g_typeParameters.UID_APP);

    nvs_set_u8(nvs_handle, "network_state",    g_typeParameters.networkState);

    nvs_commit(nvs_handle);

    nvs_close(nvs_handle);

    return 0;
}

int8_t cParametersRead(void)
{
    nvs_handle_t nvs_handle;
    size_t size;

    nvs_open("Parameters", NVS_READONLY, &nvs_handle);

    size = sizeof(g_typeParameters.BT_NAME);
    nvs_get_str(nvs_handle, "bt_name",         g_typeParameters.BT_NAME, &size);

    size = sizeof(g_typeParameters.WIFI_STA_SSID);
    nvs_get_str(nvs_handle, "wifi_sta_ssid",   g_typeParameters.WIFI_STA_SSID, &size);

    size = sizeof(g_typeParameters.WIFI_STA_PASSWORD);
    nvs_get_str(nvs_handle, "wifi_sta_passwd", g_typeParameters.WIFI_STA_PASSWORD, &size);

    size = sizeof(g_typeParameters.WIFI_AP_SSID);
    nvs_get_str(nvs_handle, "wifi_ap_ssid",    g_typeParameters.WIFI_AP_SSID, &size);

    size = sizeof(g_typeParameters.WIFI_AP_PASSWORD);
    nvs_get_str(nvs_handle, "wifi_ap_passwd",  g_typeParameters.WIFI_AP_PASSWORD, &size);

    size = sizeof(g_typeParameters.UID_APP);
    nvs_get_str(nvs_handle, "app_uid",         g_typeParameters.UID_APP, &size);

    nvs_get_u8(nvs_handle, "network_state",    &g_typeParameters.networkState);

    nvs_close(nvs_handle);

    return 0;
}

int8_t cParametersDeinit(void)
{
    char cDatas[32] = {0};

    memset(&g_typeParameters, 0, sizeof(g_typeParameters));

    strcpy(g_typeParameters.PID, "NULL");
    strcpy(g_typeParameters.UID_DRIVER, "NULL");
    strcpy(g_typeParameters.UID_APP, "NULL");
    strncpy(g_typeParameters.UID, pcUIDGet(), sizeof(g_typeParameters.UID));

    snprintf(cDatas, sizeof(cDatas), "DBS_%s", g_typeParameters.UID);

    strncpy(g_typeParameters.BT_NAME, cDatas, sizeof(g_typeParameters.BT_NAME));

    strncpy(g_typeParameters.WIFI_AP_SSID, cDatas, sizeof(g_typeParameters.WIFI_AP_SSID));
    strcpy(g_typeParameters.WIFI_AP_PASSWORD, "12345678");

    strcpy(g_typeParameters.WIFI_STA_SSID, "tom");
    strcpy(g_typeParameters.WIFI_STA_PASSWORD, "xsb123321");

    g_typeParameters.frameIotMax = 512;
    g_typeParameters.frameMcuMax = 512;
    g_typeParameters.networkState = 0;

    return 0;
}

void vParametersInit(void)
{
    cParametersDeinit();

    /* 读取参数 */
    cParametersRead();
}

ParametersType *ptypeParametersInfoGet(void)
{
    return &g_typeParameters;
}

